package functions.ems_base_data;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.BankBean;
import com.crc.mam.BaseCase;

import coms.Constants;
import coms.JSONToBean;
import frame.HttpHelper;
import functions.ems_claim.Claim;

/**
 * 银行信息
 * @author CHENHAILIAN6
 *
 */
public class MdBank extends BaseCase{
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(MdBank.class);
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 查询银行信息
	 * @param inputParam
	 * 必填：compCode;
	 * @return JSONArray
	 */
	public JSONArray findMdBankListByCondition(HashMap<Object,Object> inputParam) {
		logger.info(" findMdBankListByCondition ------- start -------------------");
		String currentPage = "1";
		String pageSize = "10";
		String compCode = "";
		String compName = "";
		String bankName = "";
		String fieldName = "";
		String orderBy = "";
		if(inputParam == null) {
			
		}else {
			currentPage = (String) inputParam.get("currentPage");
			if(currentPage == null) {
				currentPage = "1";
			}
			pageSize = (String) inputParam.get("pageSize");
			if(pageSize == null) {
				pageSize = "10";
			}
			compCode = (String) inputParam.get("compCode");
			if(compCode == null) {
				compCode = "";
			}
			compName = (String) inputParam.get("compName");
			if(compName == null) {
				compName = "";
			}
			bankName = (String) inputParam.get("bankName");
			if(bankName == null) {
				bankName = "";
			}
			fieldName = (String) inputParam.get("fieldName");
			if(fieldName == null) {
				fieldName = "";
			}
			orderBy = (String) inputParam.get("orderBy");
			if(orderBy == null) {
				orderBy = "";
			}
		}
		String params = "{\"page\":{\"currentPage\":"+currentPage+",\"pageSize\":"+pageSize+","
				+ "\"recordCount\":0},\"params\":{\"compCode\":\""+compCode+"\",\"compName\":\""+compName+"\","
				+ "\"bankName\":\""+bankName+"\"},\"sortDTO\":{\"fieldName\":\""+fieldName+"\",\"orderBy\":\""+fieldName+"\"}}";
		String url =Constants.FIND_MD_BANK_LIST_BY_CONDITION_URL;
		try {
			result = HttpHelper.post(params, url);
			status = HttpHelper.isOk;
			logger.info("saveClaimHeadAndLine--------"+ result);
			if(status) {
				try {
					JSONObject datas = new JSONObject(result);
					int total = datas.getInt("total");
					if(total>0) {
						resultJSONArray= datas.getJSONArray("list");
						
					}
				}catch(Exception e) {
					e.printStackTrace();
					
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logger.info(" 返回结果  ------" + status);
		logger.info(" findMdBankListByCondition ------- end -------------------");
		return resultJSONArray;
		
	}
	
	public BankBean getBank(HashMap<Object,Object> inputParam) {
		BankBean bankBean = null;
		if(resultJSONArray == null) {
			resultJSONArray = findMdBankListByCondition(inputParam);
		}
		int size = resultJSONArray.length();
		int temp = 0;
		if(size >0) {
			Random dom = new Random(size);
			temp = dom.nextInt(size -1);
		}
		JSONObject object = resultJSONArray.getJSONObject(temp);
		try {
			bankBean = JSONToBean.parseJson2Object(BankBean.class, object);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info(bankBean.toString());
		return bankBean;
	
	}

}
